package jn.chen.utils;

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class TEST1 {
    private static int width = 0;
    private static int height = 0;
    public static void main(String[] args) throws InterruptedException {
        String url = "http://www.customs.gov.cn/customs/302249/302266/index.html";
        //新闻列表页面
        WebDriver webDriver = getHtmlBySelinum(url);
        Thread.sleep(8000);
        String pageSource = webDriver.getPageSource();
        // 获取到要闻新闻列表
        List<WebElement> webElements = webDriver.findElements(By.xpath("//li"));
//        WebDriverWait wait = new WebDriverWait(webDriver, 20);
//        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("main")));

    }
    /**
     * 获取页面所有内容 selinum
     * @param strURL
     * @return
     * @throws Exception
     */
    public static WebDriver getHtmlBySelinum(String strURL) {
        //window.navigator.webdriver 为true
        ChromeOptions chromeOptions = new ChromeOptions();
        boolean portexist = false;
        int tryTime = 3;
        while (!portexist && tryTime>0){
            try {
                portexist = NetUtil.isHostConnectable("127.0.0.1",9222);
                if(!portexist){
                    new Thread(()->{
                        try {
                            CMDUtil.runCMD("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe --disable-popup-blocking --remote-debugging-port=9222 --user-data-dir=\"C:\\Users\\adminChen\\AppData\\Local\\Google\\Chrome\\User\"  --window-size="+width+","+height+"  --window-position=-10,0 --test-type --ignore-certificate-errors");
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }).start();
                }
                tryTime--;
                TimeUnit.SECONDS.sleep(1);
            }catch (Exception e){
//                e.printStackTrace();
            }
        }
        // 版本：v88以上 设置window.navigator.webdriver为false
        chromeOptions.addArguments("--disable-blink-features=AutomationControlled");
//        chromeOptions.addArguments("--headless","--disable-gpu");
        // 关闭界面上的---Chrome正在受到自动软件的控制
        chromeOptions.addArguments("disable-infobars");
        chromeOptions.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
        // 设置 chromedirver 的存放位置
        System.getProperties().setProperty("webdriver.chrome.driver", "D:\\tmp\\chromedriver.exe");
        // 移除 控制文字
//        chromeOptions.setExperimentalOption("useAutomationExtension", false);
//        chromeOptions.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));

//        Map<String, Object> headers = new HashMap<>();
//        headers.put("User-Agent", "Mozilla/5.0");  // 将 "Your_User_Agent_String" 替换为您想要设置的实际用户代理字符串
//        chromeOptions.setCapability("webdriver.chrome.requestheaders", headers);

        WebDriver webDriver = new ChromeDriver(chromeOptions);
//        DesiredCapabilities des = DesiredCapabilities.chrome();
//        des.setCapability("pageLoadStrategy","none");
        webDriver.get(strURL);
//        JavascriptExecutor js = (JavascriptExecutor) webDriver;
//        js.executeScript("navigator.userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'");
        return webDriver;
    }

}
